package com.laviedesfons.jhmtasc.types.abstracts;

import com.laviedesfons.jhmtasc.exceptions.MException;
import com.laviedesfons.jhmtasc.metadata.MetaField;
import com.laviedesfons.jhmtasc.types.interfaces.InternalType;
import fr.x9c.cadmium.kernel.Value;
import java.util.ArrayList;

/**
 *
 * @author Romano
 */
public abstract class Closure extends AbstractType<ArrayList<InternalType>> {

    public InternalType getClosureValue(int idx) {
        return wrapped.get(idx);
    }

    @Override
    public AbstractType fromValue(Value v) throws MException {
        if (!v.isBlock()) {
            return this;
        }

        wrapped = new ArrayList<InternalType>();
        if (v.asBlock().sizeValues() > 0) {
            for (int id = 0; id < getMetaModel().getFieldsLength(); id++) {
                Value curVal = v.asBlock().get(id);
                if (curVal.isBlock()) {
                    MetaField field = getMetaModel().getValueField(id);
                    wrapped.add(field.getMetaType().create().fromValue(curVal));
                }
            }
        }
        return this;
    }

    @Override
    public Value toValue() {
        throw new UnsupportedOperationException("not implemented yet");
    }
}